package cn.lg.soar.mvc.aspect;

import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.exceptions.PermissionException;
import cn.lg.soar.common.util.current.ThreadContext;
import cn.lg.soar.mvc.annotations.DisabledTenantAccess;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

@ConditionalOnProperty(name = "lg.database.enabled-tenant", havingValue = "true")
@Aspect
public class DisabledTenantAccessAspect {

    @Around("@annotation(disabledTenantAccess)")
    public Object checkTenantAccess(ProceedingJoinPoint joinPoint, DisabledTenantAccess disabledTenantAccess) throws Throwable {
        if (ThreadContext.getTenantId() == 0) {
            return joinPoint.proceed();
        } else {
            throw new PermissionException(I18n.build("租户用户禁止访问"));
        }
    }

}